/*
 * @Description: loading
 * @Author:
 * @Date:
 * @LastEditors: denghong
 */

/* 参考 https://blog.csdn.net/ccc_cccc_ccc/article/details/106335915 */

import { Loading } from "element-ui";
import { debounce } from "@/utils/debounce";

let needLoadingRequestCount = 0;
let loading;

function startLoading() {
	loading = Loading.service({
		lock: true,
		text: "加载中,请稍等……",
		background: "rgba(0, 0, 0, 0.3)",
	});
}

function endLoading() {
	loading.close();
}

const tryCloseLoading = () => {
	if (needLoadingRequestCount === 0) endLoading();
};

export function showFullScreenLoading() {
	if (needLoadingRequestCount === 0) startLoading();
	needLoadingRequestCount++;
}

export function tryHideFullScreenLoading() {
	if (needLoadingRequestCount <= 0) return;
	needLoadingRequestCount--;
	if (needLoadingRequestCount === 0) debounce(tryCloseLoading, 300)();
	// 创建一个 debounced（防抖动）函数，该函数会从上一次被调用后，延迟 wait 毫秒后调用 func 方法
}
